$(function(){
	var admin_id = getCookie('admin_id');
	if(!admin_id){
		window.location.replace('login.html');
	}
	list_value();
    renderPager();
	$("#search").click(function(){
		$("#value").empty();
		list_value();
        renderPager();
	});
	//回车搜索
	$("#name").keyup(function(e){
		if(e.keyCode === 13){
			$("#search").click();
			return false;
		}
	});
	$("#mnemonic").keyup(function(e){
		if(e.keyCode === 13){
			$("#search").click();
			return false;
		}
	});
	$("#barCode").keyup(function(e){
		if(e.keyCode === 13){
			$("#search").click();
			return false;
		}
	});
	$("#articleNo").keyup(function(e){
		if(e.keyCode === 13){
			$("#search").click();
			return false;
		}
	});
});
function renderPager(){
	var articleNo=$("#articleNo").val();
	var barCode=$("#barCode").val();
	var name=$("#name").val();
	var mnemonic=$("#mnemonic").val();
	$.ajax({
		url: ApiUrl + "/goods?limit="+Page+"&articleNo="+articleNo+"&barCode="+barCode+"&name="+name+"&mnemonic="+mnemonic,
        type: 'get',
        dataType: 'JSON',
        success: function(result) {
        	if(result.code == 200){
        		var pageCount = result.data.totalPage;
        		//生成分页按钮
				if(pageCount>5){
					page_icon(1,5,0,pageCount);
				}else{
					page_icon(1,pageCount,0,pageCount);
				}
        	}else{
	    		parent.layer.msg(result.message);
	    		return false;
	    	}
        }
    });
}
function list_value(page){
	var articleNo=$("#articleNo").val();
	var barCode=$("#barCode").val();
	var name=$("#name").val();
	var mnemonic=$("#mnemonic").val();
	if(!page){
		//获取商品列表
	    $.ajax({
			url: ApiUrl + "/goods?limit="+Page+"&articleNo="+articleNo+"&barCode="+barCode+"&name="+name+"&mnemonic="+mnemonic,
	        type: 'get',
	        dataType: 'JSON',
	        success: function(result) {
	        	if(result.code == 200){
	        		var html='';
	        		$.each(result.data.list, function(k, v) {
	        			html+='<tr>'
	                            +'<td>'+v.goods_id+'</td>'
	                            +'<td>'+v.zh_name+'（'+v.en_name+'）</td>'
	                            +'<td>'+v.article_no+'</td>'
	                            +'<td>'+v.bar_code+'</td>'
	                            +'<td>'+v.mnemonic+'</td>'
	                            +'<td>'+v.categoryName+'</td>'
                                +'<td>'+v.taocangName+'</td>'
	                            +'<td>'+v.brand+'</td>'
	                            +'<td>'+v.price+'</td>'
	                            +'<td>'+v.tax+'</td>'
	                            +'<td>'+v.inventory+'</td>';
	                            if(v.status == 1){
			                    	html+='<td>正常</td>';
			                    }else{
			                    	html+='<td>下架</td>';
			                    }
                       html+='<td>'+v.created_at+'</td>'
	                            +'<td><a title = "编辑商品"  href="goods_edit.html?id='+v.goods_id+'" class="btn btn-primary btn-circle"><i class="fa fa-list"></i></a>';
                       if(v.status == 1){
                       		html+='<td><a title = "下架商品"  onclick="updatastatus('+v.goods_id+',2)" class="btn btn-primary btn-circle"><i class="fa fa-list"></i></a>'
	                            +'</td>'
	                        + '</tr>';	
                       }else{
                       		html+='<td><a title = "上架商品"  onclick="updatastatus('+v.goods_id+',1)" class="btn btn-primary btn-circle"><i class="fa fa-list"></i></a>'
	                            +'</td>'
	                        + '</tr>';
                       }
	        		});
	        		$("#value").html(html);
	        	}else{
		    		parent.layer.msg(result.message);
		    		return false;
		    	}
	        }
	    });
	}else{
		//$("#value").empty();
		//获取商品列表
	    $.ajax({
			url: ApiUrl + "/goods?limit="+Page+"&page="+page+"&articleNo="+articleNo+"&barCode="+barCode+"&name="+name+"&mnemonic="+mnemonic,
	        type: 'get',
	        dataType: 'JSON',
	        success: function(result) {
	        	if(result.code == 200){
	        		var html='';
	        		$.each(result.data.list, function(k, v) {
	        			html+='<tr>'
	                            +'<td>'+v.goods_id+'</td>'
	                            +'<td>'+v.zh_name+'（'+v.en_name+'）</td>'
	                            +'<td>'+v.article_no+'</td>'
	                            +'<td>'+v.bar_code+'</td>'
	                            +'<td>'+v.mnemonic+'</td>'
	                            +'<td>'+v.categoryName+'</td>'
	                            +'<td>'+v.taocang_id+'</td>'
	                            +'<td>'+v.brand+'</td>'
	                            +'<td>'+v.price+'</td>'
	                            +'<td>'+v.tax+'</td>'
	                            +'<td>'+v.inventory+'</td>';
	                            if(v.status == 1){
			                    	html+='<td>正常</td>';
			                    }else{
			                    	html+='<td>下架</td>';
			                    }
                       html+='<td>'+v.created_at+'</td>'
	                            +'<td><a title = "编辑商品"  href="goods_edit.html?id='+v.goods_id+'" class="btn btn-primary btn-circle"><i class="fa fa-list"></i></a>';
                       if(v.status == 1){
                       		html+='<td><a title = "下架商品"  onclick="updatastatus('+v.goods_id+',2)" class="btn btn-primary btn-circle"><i class="fa fa-list"></i></a>'
	                            +'</td>'
	                        + '</tr>';	
                       }else{
                       		html+='<td><a title = "上架商品"  onclick="updatastatus('+v.goods_id+',1)" class="btn btn-primary btn-circle"><i class="fa fa-list"></i></a>'
	                            +'</td>'
	                        + '</tr>';
                       }
                       
	        		});
	        		$("#value").html(html);
	        	}else{
		    		parent.layer.msg(result.message);
		    		return false;
		    	}
	        }
	    });
	}
	
}
function updatastatus(id,status){
	$.ajax({
		url: ApiUrl + "/goods/status/"+id,
        type: 'post',
        data:{
        	status :status
        },
        dataType: 'JSON',
        success: function(result) {
        	if(result.code == 200){
        		$(".alert").text('操作成功，页面即将自动跳转~');
	  			$(".alert").addClass('block').fadeIn(200);
	  			$(".alert").removeClass('alert-danger alert-warning alert-info alert-success').addClass('alert-success');
	  			setTimeout(function(){
	  					location.reload();
				},3000);
        	}else{
        		$(".alert").text(data.message);
	  			$(".alert").addClass('block').fadeIn(200);
	  			$(".alert").removeClass('alert-danger alert-warning alert-info alert-success').addClass('alert-danger');
	  			setTimeout(function(){
					$(".alert").removeClass('block');
					$("#alert").css('display','none'); 
				},3000);
        	}
    	}
    })
}
